package com.tianpengtech.admin.account;
import java.util.Map;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.admin.IAdminInfoEdit;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.model.role.Role;
import com.tianpengtech.common.search.EqualCondition;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.util.XxxUtil;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.other.MustBeNull;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;
import com.tianpengtech.common.validator.rules.string.StringMustBeEmail;
import com.tianpengtech.common.validator.rules.string.StringMustBeEqual;
import com.tianpengtech.common.validator.rules.string.StringMustBeMobile;
import com.tianpengtech.common.validator.rules.string.StringMustBeRequired;
import org.apache.commons.lang.StringUtils;

public class AccountAdminServiceImpl extends AbstractAdmin implements IAdminInfoEdit{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

		Account account = AccountDao.ME.queryAccountByUserName(model.getStr(Account.USERNAME));
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(model.getStr(Account.USERNAME),"用户名只能输入1-50个字符信息",1,50));
		validator.addRule(new StringMustBeEmail(model.getStr(Account.EMAIL), "必须是电子邮箱"));
		validator.addRule(new StringMustBeRequired(model.getStr(Account.ROLE), "角色必须要填写"));
		validator.addRule(new StringMustBeMobile(model.getStr(Account.MOBILE), "必须是合法的手机号码"));
		validator.addRule(new MustBeNull(account,"当前用户名已经存在了,请更换后在进行当前操作"));
		validator.excute();
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(model.getStr(Account.USERNAME),"用户名只能输入1-50个字符信息",1,50));
		validator.addRule(new StringMustBeEmail(model.getStr(Account.EMAIL),"必须是电子邮箱"));
		validator.addRule(new StringMustBeRequired(model.getStr(Account.ROLE),"角色必须要填写"));
		validator.addRule(new StringMustBeMobile(model.getStr(Account.MOBILE),"必须是合法的手机号码"));
		validator.addRule(new NumbericMustBeGreaterThan(model.getLong(Account.ID),0,"ID必须大于零"));
		validator.excute();
	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {
		if(StringUtils.isNotEmpty(model.getStr(Account.USERNAME))){
			queryConstruct.and(new LikeCondition(Account.USERNAME,"%"+model.getStr(Account.USERNAME)+"%"));
		}
	}

	@Override
	public String tableName() {
		return Account.dao.tableName();
	}

	@Override
	public void resetPass(Model<?> model,String oldPass,String newPass, String confirmPass, Kv params) throws ValidateException {
		
		String dbPass = model.getStr(Account.USERPASS);
		oldPass = XxxUtil.passMd5(oldPass);
		Validator validator = new Validator();
		validator.addRule(new StringMustBeRequired(oldPass,"账户密码不能唯空"))
				.addRule(new StringMustBeRequired(newPass,"新密码不能唯空"))
				.addRule(new StringMustBeRequired(confirmPass,"确认密码不能唯空"))
				.addRule(new StringMustBeEqual(newPass, confirmPass,"新密码必须与确认密码一致"))
				.addRule(new StringMustBeEqual(oldPass, dbPass,"当前旧密码输入不正确"))
				.excute();
		model.set(Account.USERPASS, XxxUtil.passMd5(newPass));
		model.update();
	}

}